{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "procesing ./All_song/Song1/\n",
      "move B_fix.mid ./All_song/Song1/\n",
      "procesing ./All_song/Song2/\n",
      "move B_fix.mid ./All_song/Song2/\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import csv\n",
    "import numpy as np\n",
    "from os import listdir\n",
    "from os.path import isfile, join\n",
    "import os\n",
    "\n",
    "'''\n",
    "Usage:\n",
    "1.put all song dirs into all_song_dir\n",
    "2.each song dir should contain A.mid,B.mid\n",
    "3.Result:create out_name.mid in each dir,which is fixed pitchbend midi file\n",
    "'''\n",
    "\n",
    "def fix_pitchbend(A_name,B_name,out_name):\n",
    "    '''\n",
    "    input : 2 csv file \n",
    "    A_name:file before modifying,\n",
    "    B_name:file after modifying\n",
    "    output: 1 csv file\n",
    "    fix pitchbend\n",
    "    '''\n",
    "    fA = open(A_name, 'r')\n",
    "    fB = open(B_name, 'r')\n",
    "    wf = open(out_name, 'w')\n",
    "\n",
    "    A_data = list(csv.reader(fA))\n",
    "    B_data = list(csv.reader(fB))\n",
    "    rows = len(A_data)\n",
    "    # print(A_data[:][0])\n",
    "    mod_flag = False\n",
    "    del_tone = 0\n",
    "    PBS = 2\n",
    "    cent_per_tone = 8192/PBS\n",
    "\n",
    "    for t in range(0,rows):\n",
    "        if 'Note_on' in A_data[t][2]:\n",
    "            if A_data[t][4]!=B_data[t][4]:\n",
    "                del_tone = int(B_data[t][4])-int(A_data[t][4])\n",
    "                mod_flag=True\n",
    "                shift_cent = int(cent_per_tone*del_tone)\n",
    "        elif 'Note_off' in A_data[t][2]:\n",
    "            mod_flag=False\n",
    "        elif 'Pitch_bend_c' in A_data[t][2] and mod_flag:\n",
    "            B_data[t][4] = str(np.clip(int(B_data[t][4])+shift_cent ,0,16383))\n",
    "        wf.write(','.join(B_data[t]))\n",
    "        wf.write('\\n')\n",
    "    fA.close()\n",
    "    fB.close()\n",
    "    wf.close()\n",
    "    \n",
    "\n",
    "#================= start user params =================\n",
    "all_song_dir = 'All_song'\n",
    "A_name = 'A'\n",
    "B_name = 'B'\n",
    "out_name = 'B_fix'\n",
    "#================== end user params   =================\n",
    "A_mid_name = A_name + '.mid'\n",
    "B_mid_name = B_name + '.mid'\n",
    "for f in listdir(all_song_dir):\n",
    "    song_path = str('./'+all_song_dir+'/'+f+'/')\n",
    "    proj_files = [ff for ff in listdir(song_path) if isfile(join(song_path, ff))]\n",
    "    for pf in  proj_files:\n",
    "        if pf == A_mid_name or pf ==B_mid_name:\n",
    "            csv_name = pf.replace('.mid','')+'.csv'\n",
    "            os.system('Midicsv '+song_path+pf+' '+csv_name)\n",
    "    print('procesing '+song_path)\n",
    "    fix_pitchbend(A_name+'.csv',B_name+'.csv',out_name+'.csv')\n",
    "    \n",
    "    os.system('Csvmidi '+out_name+'.csv '+out_name+'.mid')\n",
    "    if ~( os.system('move '+out_name +'.mid '+song_path)):\n",
    "        print('move '+out_name +'.mid '+song_path)\n",
    "   \n",
    "    \n",
    "os.system('del '+A_name+'.csv')\n",
    "os.system('del '+B_name+'.csv')\n",
    "os.system('del '+out_name+'.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
